package giab.weixin.web.runner;

import giab.weixin.web.putmessage.ClientSendMessage;
import giab.weixin.web.queue.MessageQueue;
import giab.weixin.web.xml.bean.Message;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zyf
 * 
 */
public class SendClientMessage implements Runnable {

	private static Logger logger = Logger.getLogger(SendClientMessage.class);
	
	@Autowired
	private ClientSendMessage csm;
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				while (MessageQueue.size()>0) {
					Message msg = MessageQueue.getMessagePeek();
					//执行发送消息的操作
					if(csm.sendMessage(msg)){//成功发送
						MessageQueue.getMessagePoll();
					}
					else {//发送失败
						logger.error("发送消息失败");
						logger.error(msg);
					}
					
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				logger.error(e);
			}

		}
	}

}
